package xgenerator.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;

public class MultiDataSourceTransaction implements Transaction {

	/**
	 * JDBC Connection
	 */
	private Vector<Connection> connVector;

	public MultiDataSourceTransaction(Connection[] conns) {
		if (null == conns) {
			throw new RuntimeException("JDBC Connections is null");
		}
		this.connVector = new Vector<Connection>();
		for(int i=0; i<conns.length; i++) {
			this.connVector.add(conns[i]);
		}
	}
	
	/**
	 * constructor
	 * 
	 * @param conns
	 */
	public MultiDataSourceTransaction(Vector<Connection> connVector) {
		if (null == connVector) {
			throw new RuntimeException("JDBC Connections is null");
		}
		this.connVector = new Vector<Connection>();
		this.connVector.addAll(connVector);
	}

	/**
	 * <p>
	 * Title：判断事务是否有效
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 * @see xgenerator.jdbc.Transaction#isActive()
	 */
	public boolean isActive() {
		boolean isActive = true;

		if (!this.connVector.isEmpty()) {
			for (int i = 0; i < this.connVector.size(); i++) {
				if (null != this.connVector.get(i)) {
					try {
						if (this.connVector.get(i).isClosed()) {
							isActive = false;
						}
					} catch (SQLException e) {
						isActive = false;
					}
				}
			}
		}

		return isActive;
	}

	/**
	 * <p>
	 * Title：Begin Transaction
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @see xgenerator.jdbc.Transaction#begin()
	 */
	public void begin() {
		if (!this.connVector.isEmpty()) {
			for (int i = 0; i < this.connVector.size(); i++) {
				if (null != this.connVector.get(i)) {
					try {
						this.connVector.get(i).setAutoCommit(false);
					} catch (SQLException e) {
						throw new RuntimeException(e);
					}
				}
			}
		}

	}

	/**
	 * <p>
	 * Title：Commit Transaction
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @see xgenerator.jdbc.Transaction#commit()
	 */
	public void commit() {
		if (!this.connVector.isEmpty()) {
			for (int i = 0; i < this.connVector.size(); i++) {
				if (null != this.connVector.get(i)) {
					try {
						this.connVector.get(i).commit();
						this.connVector.get(i).setAutoCommit(true);
					} catch (SQLException e) {
						throw new RuntimeException(e);
					}
				}
			}
		}
	}

	/**
	 * <p>
	 * Title：Rollback Transaction
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @see xgenerator.jdbc.Transaction#rollback()
	 */
	public void rollback() {
		if (!this.connVector.isEmpty()) {
			for (int i = 0; i < this.connVector.size(); i++) {
				if (null != this.connVector.get(i)) {
					try {
						this.connVector.get(i).rollback();
						this.connVector.get(i).setAutoCommit(true);
					} catch (SQLException e) {
						throw new RuntimeException(e);
					}
				}
			}
		}
	}

	public void addTransaction(Transaction transaction) {
		// TODO Auto-generated method stub
		
	}

}
